import { world } from "@minecraft/server";
import { Dimensions } from "@sapi-game/constants";
import { Game, GameComponent } from "@sapi-game/main";
import { ScriptRunner } from "@sapi-game/Runner/scriptRunner";
import { DimensionIds } from "@sapi-game/utils/vanila-data";
import { WereWolfFirstDayPhase } from "@/werewolf/phases/firstDay";
import { showRoleClassicView } from "./showRoleClassic";
import { WereWolfChestLoader } from "@/werewolf/components/chestLoader";
import { ClassicViewTools } from "./func";

export class FirstDayClassicView extends GameComponent<WereWolfFirstDayPhase> {
    get players() {
        return this.context.players;
    }

    private readonly StructurePlacePos = { x: -54, y: -61, z: -109 };

    override onAttach(): void {
        //初始化中心结构
        ClassicViewTools.resetStructure();
        this.state.getComponent(WereWolfChestLoader).resetChest();
        this.runner.run(this.process.bind(this));
        this.subscribe(
            Game.events.buttonPush,
            (t) => {
                //抽身份
                this.state.reset();
                this.state.assign();
                this.state.addComponent(showRoleClassicView);
            },
            {
                loc: [-51, -59, -106],
                dimensionId: DimensionIds.Overworld,
            }
        );
    }

    async process(r: ScriptRunner) {
        const players = this.context.players;
        players.title("§a游戏开始");
        await r.wait(50);
        players.title("§e§l第一天");
        this.runner.run(async (rr) => {
            players.playSound("block.bell.hit", { volume: 0.8 });
            await rr.wait(7);
            players.playSound("block.bell.hit", { volume: 0.8 });
            await rr.wait(6);
            players.playSound("block.bell.hit", { volume: 0.8 });
        });
        await r.wait(15);
        players.forEach((p) =>
            p.player.onScreenDisplay.updateSubtitle(
                "§a§l请裁判到中间抽取玩家的身份"
            )
        );
        await r.wait(10);
        this.popSound();
        this.loadStructure();
    }

    popSound() {
        this.runner.run(async (r) => {
            let pitch = 1.0;
            for (let i = 0; i < 2; i++) {
                this.players.playSound("random.pop", { pitch: pitch });
                await r.wait(4);
                pitch += 0.1;
            }
            this.players.playSound("random.pop", { pitch: pitch });
            for (let i = 0; i < 5; i++) {
                await r.wait(3);
                pitch += 0.1;
                this.players.playSound("random.pop", { pitch: pitch });
            }
        });
    }

    loadStructure() {
        const dim = Dimensions.Overworld;
        this.runner.run(async (r) => {
            world.structureManager.place(
                "抽身份1",
                dim,
                this.StructurePlacePos
            );
            await r.wait(9);
            world.structureManager.place(
                "抽身份2",
                dim,
                this.StructurePlacePos
            );
            await r.wait(8);
            world.structureManager.place(
                "抽身份3",
                dim,
                this.StructurePlacePos
            );
            await r.wait(7);
            world.structureManager.place(
                "抽身份4",
                dim,
                this.StructurePlacePos
            );
            await r.wait(2);
            dim.runCommand(
                "particle minecraft:water_evaporation_bucket_emitter -51 -59 -106"
            );
            await r.wait(1);
            dim.runCommand(
                "particle minecraft:water_evaporation_bucket_emitter -51 -58 -107"
            );
            dim.runCommand(
                "particle minecraft:water_evaporation_bucket_emitter -51 -58 -105"
            );
            dim.runCommand(
                "particle minecraft:water_evaporation_bucket_emitter -52 -58 -106"
            );
            dim.runCommand(
                "particle minecraft:water_evaporation_bucket_emitter -50 -58 -106"
            );
        });
    }
}
